package com.xywy.wjk;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextPaint;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.app.MainApplication;
import com.xywy.common.bean.ConnectedDevice;
import com.xywy.common.greendao.BaseDao;
import com.xywy.common.util.FileUtil;
import com.xywy.common.util.ble.util.BLEBloodPresureProvider;
import com.xywy.me.bean.User;
import com.xywy.me.bean.UserKeeper;
import com.xywy.wjk.bean.BloodPressureData;
import com.xywy.wjk.dao.BloodPressureDataDao;
import com.xywy.wjk.dao.BloodPressureDataDao.Properties;
import com.xywy.wjk.observer.ObserverConstants;
import com.xywy.wjk.util.WjkConstant;
import com.xywy.wjk.widget.BloodHeartView;
import com.xywy.wjk.widget.BloodZxView;
import com.xywy.wjk.widget.MyZxView;
import com.xywy.wjk.widget.WjkChart;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * @ClassName: WjkDetailActivity
 * @Description: 云健康血压详情页
 * 
 * @author ZhiluCui
 * @date 2014年10月13日 下午8:12:24
 * @version:1.0
 */
public class BloodPressureDetailActivity extends BaseActivity implements
		OnClickListener {
	private static final String TAG = "BloodPressureDetailActivity";

	private Context mContext;

	private TextView tv_wjk_detail_hbp;

	private TextView tv_yjk_detail_lbp;

	private TextView tv_yjk_detail_hr;
	private TextView tv_bp_explain;

	private RelativeLayout backRL;
	private MainApplication app;

	private BloodPressureDataDao bloodDao;
	private BloodZxView blood_pressure_chart;
	private BloodHeartView heart_rate_chart;
	private TextView btn_hr_detail_day, btn_hr_detail_week,
			btn_hr_detail_month, btn_hr_detail_year;
	private TextView addLow, addHeigh, historylowh, historyHeighh,
			historyHieghL, historyLowL;
	private TextView heartHeigh;
	private TextView heartLow;
	private TextView aveHeartTV;
	private TextView tv_add_device;

	private BLEBloodPresureProvider bleBloodPresureProvider;
	private SharedPreferences sp;
	private int temp2; // 表示wjk页面当前的fragment为 10 体重 ， 11 血压；（不确定是否连接）
    
	private User current_user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_blood_pressure_detail);
		this.preInit();
		initDAO();
		this.initView();
		this.loadData();

	}

	@Override
	protected void onResume() {
		super.onResume();
		List<BloodPressureData> datas = getLastData();
		System.out.println(datas.size() + "-------------onresume");
		if (datas != null && datas.size() > 0) {
			DecimalFormat df = new DecimalFormat("0");
			tv_wjk_detail_hbp.setText(df.format(datas.get(0).getHighPressure())
					+ "");
			tv_yjk_detail_lbp.setText(df.format(datas.get(0).getLowPressure())
					+ "");
			tv_yjk_detail_hr.setText(df.format(datas.get(0).getHeartRate())
					+ "");

			if (datas.size() > 1) {

				String temp1 = "";
				if ((datas.get(0).getLowPressure() - datas.get(1)
						.getLowPressure()) > 0) {
					temp1 = "+";
				} else {
					temp1 = "";
				}

				addLow.setText(temp1
						+ df.format((datas.get(0).getLowPressure() - datas.get(
								1).getLowPressure())));
				String temp2 = "";
				if ((datas.get(0).getHighPressure() - datas.get(1)
						.getHighPressure()) > 0) {
					temp2 = "+";
				} else {
					temp2 = "";
				}
				addHeigh.setText(temp2
						+ df.format((datas.get(0).getHighPressure() - datas
								.get(1).getHighPressure())));
			} else {
				addLow.setText(0 + "");
				addHeigh.setText(0 + "");
			}
			float heighh = 0, lowh = datas.get(0).getHighPressure(), heighl = 0, lowl = datas
					.get(0).getLowPressure();
			float heart0 = 0, heart1 = datas.get(0).getHeartRate();
			float heartCount = 0;
			for (BloodPressureData data : datas) {
				if (heighh < data.getHighPressure())
					heighh = data.getHighPressure();
				if (heighl < data.getLowPressure())
					heighl = data.getLowPressure();
				if (lowh > data.getHighPressure()
						&& data.getHighPressure() != 0)
					lowh = data.getHighPressure();
				if (lowl > data.getLowPressure() && data.getLowPressure() != 0)
					lowl = data.getLowPressure();

				if (heart0 < data.getHeartRate())
					heart0 = data.getHeartRate();
				if (heart1 > data.getHeartRate() && data.getHeartRate() != 0)
					heart1 = data.getHeartRate();
				heartCount += data.getHeartRate();

			}
			historyHeighh.setText(df.format(heighh) + ""); // 历史最低
			historylowh.setText(df.format(lowh) + "");
			historyHieghL.setText(df.format(heighl) + "");
			historyLowL.setText(df.format(lowl) + "");
			heartHeigh.setText(df.format(heart0) + "");
			heartLow.setText(df.format(heart1) + "");
			aveHeartTV.setText(df.format(heartCount / datas.size()));
		} else {
			blood_pressure_chart.setGaveWorth(getDayAgoData(),
					MyZxView.MODE_DAY);
			heart_rate_chart.setGaveWorth(getDayAgoData(), MyZxView.MODE_DAY);
			addLow.setText(0 + "");
			addHeigh.setText(0 + "");
			tv_wjk_detail_hbp.setText(0 + "");
			tv_yjk_detail_lbp.setText(0 + "");
			tv_yjk_detail_hr.setText(0 + "");
			historyHeighh.setText(0 + ""); // 历史最低
			historylowh.setText(0 + "");
			historyHieghL.setText(0 + "");
			historyLowL.setText(0 + "");
			heartHeigh.setText("" + 0);
			heartLow.setText(0 + "");
			aveHeartTV.setText(0 + "");
			tv_bp_explain.setText("");
		}
	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	public void preInit() {
		MainApplication.addSpeciall(this);

		current_user = UserKeeper.getCurrentUser(this);
		Intent intent = getIntent();
		if (intent.hasExtra(ScanBLEActivity.USER_WEIGHT_DATA_1)
				&& intent.hasExtra(ScanBLEActivity.USER_WEIGHT_DATA_2)) {
		}
		mContext = this.context;
	}

	private void initDAO() {
		BaseDao baseDao = BaseDao.getInstance(BloodPressureDetailActivity.this);
		bloodDao = baseDao.getBloodPressureDataDao();
	}

	@Override
	public void loadData() {
		app = MainApplication.getInstance();
		connectBtnState();

		blood_pressure_chart.setGaveWorth(getDayAgoData(), MyZxView.MODE_DAY);
		heart_rate_chart.setGaveWorth(getDayAgoData(), MyZxView.MODE_DAY);
		setSelect(MyZxView.MODE_DAY);
		setSelectHr(MyZxView.MODE_DAY);
		List<BloodPressureData> datas = getLastData();
		if (datas != null && datas.size() > 0) {
			DecimalFormat df = new DecimalFormat("0");
			tv_wjk_detail_hbp.setText(df.format(datas.get(0).getHighPressure())
					+ "");
			tv_yjk_detail_lbp.setText(df.format(datas.get(0).getLowPressure())
					+ "");
			tv_yjk_detail_hr.setText(df.format(datas.get(0).getHeartRate())
					+ "");

			if (datas.size() > 1) {

				String temp1 = "";
				if ((datas.get(0).getLowPressure() - datas.get(1)
						.getLowPressure()) > 0) {
					temp1 = "+";
				} else {
					temp1 = "";
				}

				addLow.setText(temp1
						+ df.format((datas.get(0).getLowPressure() - datas.get(
								1).getLowPressure())));
				String temp2 = "";
				if ((datas.get(0).getHighPressure() - datas.get(1)
						.getHighPressure()) > 0) {
					temp2 = "+";
				} else {
					temp2 = "";
				}
				addHeigh.setText(temp2
						+ df.format((datas.get(0).getHighPressure() - datas
								.get(1).getHighPressure())));
			} else {
				addLow.setText(0 + "");
				addHeigh.setText(0 + "");
			}
			float heighh = 0, lowh = datas.get(0).getHighPressure(), heighl = 0, lowl = datas
					.get(0).getLowPressure();
			float heart0 = 0, heart1 = datas.get(0).getHeartRate();
			float heartCount = 0;
			for (BloodPressureData data : datas) {
				if (heighh < data.getHighPressure())
					heighh = data.getHighPressure();
				if (heighl < data.getLowPressure())
					heighl = data.getLowPressure();
				if (lowh > data.getHighPressure()
						&& data.getHighPressure() != 0)
					lowh = data.getHighPressure();
				if (lowl > data.getLowPressure() && data.getLowPressure() != 0)
					lowl = data.getLowPressure();

				if (heart0 < data.getHeartRate())
					heart0 = data.getHeartRate();
				if (heart1 > data.getHeartRate() && data.getHeartRate() != 0)
					heart1 = data.getHeartRate();
				heartCount += data.getHeartRate();

			}
			historyHeighh.setText(df.format(heighh) + ""); // 历史最低
			historylowh.setText(df.format(lowh) + "");
			historyHieghL.setText(df.format(heighl) + "");
			historyLowL.setText(df.format(lowl) + "");
			heartHeigh.setText(df.format(heart0) + "");
			heartLow.setText(df.format(heart1) + "");
			aveHeartTV.setText(df.format(heartCount / datas.size()));
		} else {
		}
	}

	/**
	 * 当前页面体重连接的一个显示隐藏情况；
	 */
	private void connectBtnState() {
		bleBloodPresureProvider = MainApplication.getInstance()
				.getBloodPBleProvider();
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		temp2 = sp.getInt("curFrag", -2);
		if (app.getBloodPBleProvider().getConnectState() == BluetoothProfile.STATE_CONNECTED) {
			if (temp2 == 10) {
				tv_add_device.setVisibility(View.VISIBLE);
			} else {
				tv_add_device.setVisibility(View.GONE);
			}
		} else {
			tv_add_device.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void initView() {
		backRL = (RelativeLayout) findViewById(R.id.rl_activity_bp_detail_back);
		backRL.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		tv_add_device = (TextView) findViewById(R.id.tv_add_device);
		tv_add_device.setOnClickListener(this);
		tv_wjk_detail_hbp = (TextView) findViewById(R.id.tv_wjk_detail_hbp);
		TextPaint htp = tv_wjk_detail_hbp.getPaint();
		htp.setFakeBoldText(true);
		tv_yjk_detail_lbp = (TextView) findViewById(R.id.tv_yjk_detail_lbp);
		TextPaint ltp = tv_yjk_detail_lbp.getPaint();
		ltp.setFakeBoldText(true);
		tv_yjk_detail_hr = (TextView) findViewById(R.id.tv_yjk_detail_hr);
		TextPaint hrtp = tv_yjk_detail_hr.getPaint();
		hrtp.setFakeBoldText(true);
		tv_bp_explain = (TextView) findViewById(R.id.tv_bp_explain);
		List<BloodPressureData> list = getLastData();
		if (list != null && list.size() > 0) {
			// BloodPresureCommon.getTestResult(list.get(0).getHighPressure(),
			// list.get(0).getLowPressure());
			tv_bp_explain.setText(getResult(list.get(0).getHighPressure(),
					list.get(0).getLowPressure()).toString());
		}
		SharedPreferences sp = getSharedPreferences("config",
				Context.MODE_PRIVATE);
		int height = sp.getInt("height", 0);

		btn_bp_detail_day = (TextView) findViewById(R.id.btn_bp_detail_day);
		btn_bp_detail_week = (TextView) findViewById(R.id.btn_bp_detail_week);
		btn_bp_detail_month = (TextView) findViewById(R.id.btn_bp_detail_month);
		btn_bp_detail_year = (TextView) findViewById(R.id.btn_bp_detail_year);
		blood_pressure_chart = (BloodZxView) findViewById(R.id.blood_pressure_chart);
		heart_rate_chart = (BloodHeartView) findViewById(R.id.heart_rate_chart);

		btn_bp_detail_day.setOnClickListener(this);
		btn_bp_detail_week.setOnClickListener(this);
		btn_bp_detail_month.setOnClickListener(this);
		btn_bp_detail_year.setOnClickListener(this);

		btn_hr_detail_day = (TextView) findViewById(R.id.btn_hr_detail_day);
		btn_hr_detail_week = (TextView) findViewById(R.id.btn_hr_detail_week);
		btn_hr_detail_month = (TextView) findViewById(R.id.btn_hr_detail_month);
		btn_hr_detail_year = (TextView) findViewById(R.id.btn_hr_detail_year);

		btn_hr_detail_day.setOnClickListener(this);
		btn_hr_detail_week.setOnClickListener(this);
		btn_hr_detail_month.setOnClickListener(this);
		btn_hr_detail_year.setOnClickListener(this);

		addLow = (TextView) findViewById(R.id.addLow);
		addHeigh = (TextView) findViewById(R.id.addHeigh);
		historylowh = (TextView) findViewById(R.id.historylowh);
		historyHeighh = (TextView) findViewById(R.id.historyHeighh);
		historyHieghL = (TextView) findViewById(R.id.historyHieghL);
		historyLowL = (TextView) findViewById(R.id.historyLowL);
		heartHeigh = (TextView) findViewById(R.id.heartHeigh);
		heartLow = (TextView) findViewById(R.id.heartLow);
		aveHeartTV = (TextView) findViewById(R.id.aveHeartTV);
		findViewById(R.id.bt_bp_title_back).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});

		findViewById(R.id.tv_title_back).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		blood_pressure_chart.setOnClickListener(this);
		heart_rate_chart.setOnClickListener(this);

	}

	/**
	 * 获取最后一次测量的结果给予评论
	 */
	public String getResult(float high, float low) {
		// Math.max(getHighLevel(high), getLowLevel(low));
		if (high <= 90 || low <= 60) {
			return "血压偏低，建议多饮水，增加营养。加强体育锻炼，提高身体调节功能.";
		}
		if ((high <= 120 && high > 90) || (low > 60 && low <= 80)) {
			return "血压正常，继续保持良好的情绪，健康的生活习惯。";
		}
		if ((high <= 139 && high > 120) || (low > 80 && low <= 89)) {
			return "血压正常稍高，属于正常高值。注意低盐清淡饮食，合理安排作息，保持良好的情绪。";
		}
		if ((high <= 159 && high > 140) || (low > 90 && low <= 99)) {
			return "轻度高血压，注意改善生活习惯，饮食清淡少盐，避免辛辣等刺激性食物。";
		}
		if ((high <= 179 && high > 160) || (low > 100 && low <= 109)) {
			return "中度高血压，建议低盐低脂清淡饮食，禁烟禁酒，避免情绪激动。";
		}
		if (high > 179 || low > 110) {
			return "重度高血压，建议低盐清淡饮食，禁烟禁酒，避免情绪激动；医生指导下服用降压药。";
		} else
			return "血压不太理想";

	}

	public void wjkDetailOnClick(View view) {
		switch (view.getId()) {
		case R.id.heart_rate_chart:
		case R.id.blood_pressure_chart:

		case R.id.btn_wjk_detail_day:
		case R.id.btn_wjk_detail_week:
		case R.id.btn_wjk_detail_month:
		case R.id.btn_wjk_detail_year:
			chartDetail();
			break;
		case R.id.rl_wjk_detail_back:
			BloodPressureDetailActivity.this.finish();
			break;
		}
	}

	/**
	 * 水平显示详情
	 */
	private void chartDetail() {
		Intent intent = new WjkChart().execute(mContext);
		mContext.startActivity(intent);
	}

	View.OnClickListener itemsOnClick = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.tv_wjk_plan_share_weixin:

				break;
			case R.id.tv_wjk_plan_share_xlwb:

				break;
			case R.id.tv_wjk_plan_share_qq:

				break;
			case R.id.tv_wjk_plan_share_email:

				break;
			case R.id.tv_wjk_plan_share_pyq:

				break;
			case R.id.tv_wjk_plan_share_txyb:

				break;
			}

		}
	};

	/**
	 * 获得一天的数据
	 * 
	 * @return
	 */
	private BloodPressureData[] getDayAgoData() {
		BloodPressureData[] blood;
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		Date date = cal.getTime();
		QueryBuilder<BloodPressureData> qb = bloodDao.queryBuilder();
		qb.where(Properties.User_id.eq(current_user.getUserid()));
		qb.orderAsc(Properties.Date);
		qb.where(Properties.Date.gt(date.getTime()));
		List<BloodPressureData> datas = qb.list();
		for (BloodPressureData te : datas) {
			Log.e("datas --time -- weight", new Date(te.getDate()).toString()
					+ te.getHighPressure());
		}
		blood = new BloodPressureData[24];
		long weekDate = date.getTime();

		for (int i = 0; i < datas.size(); i++) {
			long curDate = datas.get(i).getDate();
			int temp = (int) (curDate - weekDate) / (1000 * 60 * 60);
			Log.e("temp---", temp + "");
			cal.setTimeInMillis(weekDate);
			Log.e("date", cal.get(Calendar.DATE) + "");
			Log.e("temp", temp + "");
			Log.e("iiiiii", i + "");
			if (temp < blood.length && temp >= 0)
				blood[temp] = datas.get(i);
		}
		return blood;
	}

	/**
	 * 获取一周以内的数据
	 * 
	 * @return
	 */
	private BloodPressureData[] getWeekAgoData() {

		BloodPressureData[] blood = new BloodPressureData[7];

		Calendar curr = Calendar.getInstance();
		curr.set(Calendar.DAY_OF_MONTH, curr.get(Calendar.DAY_OF_MONTH) - 7);
		Date date = curr.getTime();
		QueryBuilder<BloodPressureData> qb = bloodDao.queryBuilder();
		qb.where(Properties.User_id.eq(current_user.getUserid()));
		qb.where(Properties.Date.gt(date.getTime()));
		List<BloodPressureData> datas = qb.list();
		long weekDate = date.getTime();
		
		Calendar curr_calendar = Calendar.getInstance();
		int year_current = curr_calendar.get(Calendar.YEAR);
		int day_current = curr_calendar.get(Calendar.DAY_OF_YEAR);
		for (int i = 0; i < datas.size(); i++) {
			Calendar temp_calendar = Calendar.getInstance();
			temp_calendar.setTimeInMillis(datas.get(i).getDate());
			int year_temp = temp_calendar.get(Calendar.YEAR);
			int day_temp = temp_calendar.get(Calendar.DAY_OF_YEAR);
			
			int temp = -1;
//			long curDate = datas.get(i).getDate();
//			int temp = (int) ((curDate - weekDate) / 86400000);
			if(year_temp == year_current){
				temp = day_current - day_temp;
			}else {
				temp = day_current+31-day_temp;
			}
			if (temp >= 0 && temp < blood.length)
				blood[temp] = datas.get(i);
		}
		return blood;
	}

	/**
	 * 获取一个月以内的数据
	 * 
	 * @return
	 */
	private BloodPressureData[] getMonthAgoData() {
		BloodPressureData[] blood = new BloodPressureData[31];

		Calendar curr = Calendar.getInstance();

		curr.set(Calendar.MONTH, curr.get(Calendar.MONTH) - 1);
		Date date = curr.getTime();
		QueryBuilder<BloodPressureData> qb = bloodDao.queryBuilder();
		qb.where(Properties.User_id.eq(current_user.getUserid()));
		qb.where(Properties.Date.gt(date.getTime()));

		List<BloodPressureData> datas = qb.list();

		long weekDate = date.getTime();

		
		Calendar curr_calendar = Calendar.getInstance();
		int year_current = curr_calendar.get(Calendar.YEAR);
		int day_current = curr_calendar.get(Calendar.DAY_OF_YEAR);
		for (int i = 0; i < datas.size(); i++) {
			Calendar temp_calendar = Calendar.getInstance();
			temp_calendar.setTimeInMillis(datas.get(i).getDate());
			int year_temp = temp_calendar.get(Calendar.YEAR);
			int day_temp = temp_calendar.get(Calendar.DAY_OF_YEAR);
			
			int temp = -1;
//			long curDate = datas.get(i).getDate();
//			int temp = (int) ((curDate - weekDate) / 86400000);
			if(year_temp == year_current){
				temp = day_current - day_temp;
			}else {
				temp = day_current+31-day_temp;
			}
			if (temp >= 0 && temp < blood.length)
				blood[temp] = datas.get(i);
		}
		return blood;
	}

	/**
	 * 获取一年以内的数据
	 * 
	 * @return
	 */
	private BloodPressureData[] getYearAgoData() {
		BloodPressureData[] blood = new BloodPressureData[12];

		Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, -1);
		Date date = c.getTime();
		QueryBuilder<BloodPressureData> qb = bloodDao.queryBuilder();
		qb.where(Properties.User_id.eq(current_user.getUserid()));
		qb.where(Properties.Date.gt(date.getTime()));
		List<BloodPressureData> datas = qb.list();
		long weekDate = date.getTime();

		for (int i = 0; i < datas.size(); i++) {
			long curDate = datas.get(i).getDate();
			int haha = (int) ((curDate - weekDate) / (86400 * 31));
			int temp = haha / 1000;
			if (temp >= 0 && temp < blood.length)
				blood[temp] = datas.get(i);
		}
		return blood;
	}

	/**
	 * 获取最后一条数据
	 * 
	 * @return
	 */
	private List<BloodPressureData> getLastData() {
		QueryBuilder<BloodPressureData> qb = bloodDao.queryBuilder();
		qb.where(Properties.User_id.eq(current_user.getUserid()));
		qb.orderDesc(Properties.Id);
		return qb.list();
	}

	private TextView btn_bp_detail_day, btn_bp_detail_week,
			btn_bp_detail_month, btn_bp_detail_year;

	@SuppressLint("ResourceAsColor")
	private void setSelect(int index) {

		Drawable tab_button = getResources().getDrawable(R.drawable.tab_button);
		Drawable tab_button_white = getResources().getDrawable(
				R.drawable.tab_button_white);
		int white = getResources().getColor(R.color.white);
		int light_black = getResources().getColor(R.color.light_black);
		switch (index) {
		case MyZxView.MODE_DAY:
			btn_bp_detail_day.setTextColor(white);
			btn_bp_detail_week.setTextColor(light_black);
			btn_bp_detail_month.setTextColor(light_black);
			btn_bp_detail_year.setTextColor(light_black);
			btn_bp_detail_day.setBackgroundDrawable(tab_button);
			btn_bp_detail_week.setBackgroundDrawable(tab_button_white);
			btn_bp_detail_month.setBackgroundDrawable(tab_button_white);
			btn_bp_detail_year.setBackgroundDrawable(tab_button_white);
			break;
		case MyZxView.MODE_MOUNTH:
			btn_bp_detail_day.setTextColor(light_black);
			btn_bp_detail_week.setTextColor(light_black);
			btn_bp_detail_month.setTextColor(white);
			btn_bp_detail_year.setTextColor(light_black);
			btn_bp_detail_day.setBackgroundDrawable(tab_button_white);
			btn_bp_detail_week.setBackgroundDrawable(tab_button_white);
			btn_bp_detail_month.setBackgroundDrawable(tab_button);
			btn_bp_detail_year.setBackgroundDrawable(tab_button_white);
			break;
		case MyZxView.MODE_WEEK:
			btn_bp_detail_day.setTextColor(light_black);
			btn_bp_detail_week.setTextColor(white);
			btn_bp_detail_month.setTextColor(light_black);
			btn_bp_detail_year.setTextColor(light_black);
			btn_bp_detail_day.setBackgroundDrawable(tab_button_white);
			btn_bp_detail_week.setBackgroundDrawable(tab_button);
			btn_bp_detail_month.setBackgroundDrawable(tab_button_white);
			btn_bp_detail_year.setBackgroundDrawable(tab_button_white);
			break;
		case MyZxView.MODE_YEAR:
			btn_bp_detail_day.setTextColor(light_black);
			btn_bp_detail_week.setTextColor(light_black);
			btn_bp_detail_month.setTextColor(light_black);
			btn_bp_detail_year.setTextColor(white);
			btn_bp_detail_day.setBackgroundDrawable(tab_button_white);
			btn_bp_detail_week.setBackgroundDrawable(tab_button_white);
			btn_bp_detail_month.setBackgroundDrawable(tab_button_white);
			btn_bp_detail_year.setBackgroundDrawable(tab_button);
			break;
		default:
			break;
		}
	}

	@SuppressLint("ResourceAsColor")
	private void setSelectHr(int index) {

		Drawable tab_button = getResources().getDrawable(R.drawable.tab_button);
		Drawable tab_button_white = getResources().getDrawable(
				R.drawable.tab_button_white);
		int white = getResources().getColor(R.color.white);
		int light_black = getResources().getColor(R.color.light_black);
		switch (index) {
		case MyZxView.MODE_DAY:
			btn_hr_detail_day.setTextColor(white);
			btn_hr_detail_week.setTextColor(light_black);
			btn_hr_detail_month.setTextColor(light_black);
			btn_hr_detail_year.setTextColor(light_black);
			btn_hr_detail_day.setBackgroundDrawable(tab_button);
			btn_hr_detail_week.setBackgroundDrawable(tab_button_white);
			btn_hr_detail_month.setBackgroundDrawable(tab_button_white);
			btn_hr_detail_year.setBackgroundDrawable(tab_button_white);
			break;
		case MyZxView.MODE_MOUNTH:
			btn_hr_detail_day.setTextColor(light_black);
			btn_hr_detail_week.setTextColor(light_black);
			btn_hr_detail_month.setTextColor(white);
			btn_hr_detail_year.setTextColor(light_black);
			btn_hr_detail_day.setBackgroundDrawable(tab_button_white);
			btn_hr_detail_week.setBackgroundDrawable(tab_button_white);
			btn_hr_detail_month.setBackgroundDrawable(tab_button);
			btn_hr_detail_year.setBackgroundDrawable(tab_button_white);
			break;
		case MyZxView.MODE_WEEK:
			btn_hr_detail_day.setTextColor(light_black);
			btn_hr_detail_week.setTextColor(white);
			btn_hr_detail_month.setTextColor(light_black);
			btn_hr_detail_year.setTextColor(light_black);
			btn_hr_detail_day.setBackgroundDrawable(tab_button_white);
			btn_hr_detail_week.setBackgroundDrawable(tab_button);
			btn_hr_detail_month.setBackgroundDrawable(tab_button_white);
			btn_hr_detail_year.setBackgroundDrawable(tab_button_white);
			break;
		case MyZxView.MODE_YEAR:
			btn_hr_detail_day.setTextColor(light_black);
			btn_hr_detail_week.setTextColor(light_black);
			btn_hr_detail_month.setTextColor(light_black);
			btn_hr_detail_year.setTextColor(white);
			btn_hr_detail_day.setBackgroundDrawable(tab_button_white);
			btn_hr_detail_week.setBackgroundDrawable(tab_button_white);
			btn_hr_detail_month.setBackgroundDrawable(tab_button_white);
			btn_hr_detail_year.setBackgroundDrawable(tab_button);
			break;
		default:
			break;
		}
	}

	public void bpDetailOnClick() {

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.btn_bp_detail_day:
			blood_pressure_chart.setGaveWorth(getDayAgoData(),
					MyZxView.MODE_DAY);
			setSelect(MyZxView.MODE_DAY);
			break;
		case R.id.btn_bp_detail_week:
			blood_pressure_chart.setGaveWorth(getWeekAgoData(),
					MyZxView.MODE_WEEK);
			setSelect(MyZxView.MODE_WEEK);
			break;
		case R.id.btn_bp_detail_month:
			blood_pressure_chart.setGaveWorth(getMonthAgoData(),
					MyZxView.MODE_MOUNTH);
			setSelect(MyZxView.MODE_MOUNTH);
			break;
		case R.id.btn_bp_detail_year:
			blood_pressure_chart.setGaveWorth(getYearAgoData(),
					MyZxView.MODE_YEAR);
			setSelect(MyZxView.MODE_YEAR);
			break;
		case R.id.btn_hr_detail_day:
			heart_rate_chart.setGaveWorth(getYearAgoData(), MyZxView.MODE_DAY);
			setSelectHr(MyZxView.MODE_DAY);
			break;
		case R.id.btn_hr_detail_week:
			heart_rate_chart.setGaveWorth(getYearAgoData(), MyZxView.MODE_WEEK);
			setSelectHr(MyZxView.MODE_WEEK);
			break;
		case R.id.btn_hr_detail_month:
			heart_rate_chart.setGaveWorth(getYearAgoData(),
					MyZxView.MODE_MOUNTH);
			setSelectHr(MyZxView.MODE_MOUNTH);
			break;
		case R.id.btn_hr_detail_year:
			heart_rate_chart.setGaveWorth(getYearAgoData(), MyZxView.MODE_YEAR);
			setSelectHr(MyZxView.MODE_YEAR);
			break;
		case R.id.heart_rate_chart:
		case R.id.blood_pressure_chart:
			Intent intent = new Intent(this, BloodAllDataActivity.class);
			startActivity(intent);
			break;
		case R.id.tv_add_device:
			MobclickAgent.onEvent(mContext, "A004");
//			if (app.getWeightBleProvider() != null) {
//				app.getWeightBleProvider().disconnect();
//			}
//			if (temp2 == 10) { // 可能当前血压计正在连接 ，所以先执行一次断开方法
//				bleBloodPresureProvider.disconnect();
//			}
//			getContentResolver().notifyChange(ObserverConstants.CHANGE_BLOOD,
//					null);
			
			ConnectedDevice weight_device = FileUtil.getWeightDevice();
			if(weight_device.isConnected()){
				Intent intent1 = new Intent(WjkConstant.CONNECT_BLOOD_ACTION_DISCONNET);
				sendBroadcast(intent1);
			}else {
				Intent intent1 = new Intent(WjkConstant.CONNECT_BLOOD_ACTION);
				sendBroadcast(intent1);
			}
			
			MainApplication.finishSpeciall();
			break;
		default:
			break;
		}
	}
}
